<?php

namespace app\admin\model;

use think\Model;

use traits\model\SoftDelete;
class Meeting extends Model
{

    

    use SoftDelete;

    // 表名
    protected $name = 'meeting';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'meetingtime_text',
        'status_text',
        'user_id_text',
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }
    public function admin()
    {
        return $this->hasOne('admin','id','user_id');
    }

    public function getMeetingtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['meetingtime']) ? $data['meetingtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setMeetingtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

   //修改用户id
   protected function setUserIDAttr($value)
   {
       //有可能是excel提交的，有可能是添加的 都是字符串
       //只有数字和逗号
       $par = '/^[(0-9)(\,)+]+$/';
       if(!preg_match($par, $value)){
           $value = explode(',',$value);
           foreach($value as $v){
               $arr[] = Admin::where('nickname',$v)->value('id');
           }
           return implode(',',$arr);
       }
       return $value;
   }

   public function getUserIDTextAttr($value,$row){

       $arr_value = explode(',',$row['user_id']);
       foreach($arr_value as $v){
           $arr[] = Admin::where('id',$v)->value('nickname');
       }
       return implode(',',$arr);
   }






}
